package com.maduar.batch.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

/**
 * Description:
 *
 * @author maduar maduar@163.com
 * @date 2021-07-09 23:31
 */
@Slf4j
@Configuration
@EnableBatchProcessing
public class SplitJob {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    private String jobName = "splitJob";
    private String step1 = "splitJob-step1";
    private String step2 = "splitJob-step2";
    private String step3 = "splitJob-step3";

    private String flow1 = "splitJob-flow1";
    private String flow2 = "splitJob-flow2";


    @Bean
    public Step splitDemoStep1() {
        return stepBuilderFactory.get(step1)
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        log.info(step1);
                        return RepeatStatus.FINISHED;
                    }
                })
                .build();
    }

    @Bean
    public Step splitDemoStep2() {
        return stepBuilderFactory.get(step2)
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        log.info(step2);
                        return RepeatStatus.FINISHED;
                    }
                })
                .build();
    }

    @Bean
    public Step splitDemoStep3() {
        return stepBuilderFactory.get(step3)
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        log.info(step3);
                        return RepeatStatus.FINISHED;
                    }
                })
                .build();
    }

    @Bean
    public Flow splitFlow1() {
        return new FlowBuilder<Flow>(flow1)
                .start(splitDemoStep1())
                .build();
    }

    @Bean
    public Flow splitFlow2() {
        return new FlowBuilder<Flow>(flow2)
                .start(splitDemoStep2())
                .next(splitDemoStep3())
                .build();
    }

    @Bean
    public Job splitJobMethod() {
        return jobBuilderFactory.get(jobName)
                .start(splitFlow1())
                .split(new SimpleAsyncTaskExecutor()).add(splitFlow2())
                .end()
                .build();
    }
}
